using System;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Presentation.Commands;
using Shatkotha.WPF.Global.Bases;
using Shatkotha.WPF.Global.Data;
using Shatkotha.WPF.Global.Data.BLL;
using Shatkotha.WPF.Global.Modules.Contacts;
using Microsoft.Practices.Unity;
using Shatkotha.WPF.Global.Common.Controls.Dialog;
using Shatkotha.WPF.Global.Common.Events;

namespace Shatkotha.WPF.Contacts.Presenters
{
    public class ContactPresenter:APresenter,IContactPresenter
    {
        public ContactPresenter(IContactView view,IUnityContainer container,IEventAggregator eventAggregator) : base(view)
        {
            EventAggregator = eventAggregator;
            Container = container;
            AddNewContactCommand =  new DelegateCommand<object>(ExecuteAddContact);
        }

        public DelegateCommand<object> AddNewContactCommand { get; set; }

        public IUnityContainer Container { get; set; }

        public IEventAggregator EventAggregator { get; set; }

        private void ExecuteAddContact(object obj)
        {
            var dialogContentPresenter = Container.Resolve<IContactEditorPresenter>();
            dialogContentPresenter.Person = new Person();
            if(Container.Resolve<IDialogPresenter>().ShowModal(dialogContentPresenter,"Add Contenct",DialogButton.OKCancel)== DialogResult.OK)
            {
                var personManager = Container.Resolve<IPersonManager>();
                IPerson person = personManager.CreateNewPerson(dialogContentPresenter.Person);
                EventAggregator.GetEvent<PersonAddedEvent>().Publish(person);
            }
        }
    }
}